############
# packages #
############
library(tidyverse)
library(dbplyr)
library(dplyr)
library(vdemdata)

################
# data mergers #
################
### Basis: V-Party V2
## load V-Party data
dat.vparty.full <- vparty
## subset by year + relevant variables
vparty.vars <- c("v2paid","v2pavote","v2paallian","v2pariglef_ord","v2paimmig_ord","v2pawomlab_ord",
                 "v2panom_ord","v2paenname","v2paorname","v2pashname","country_id","country_text_id",
                 "year","pf_party_id","COWcode","country_name","v2pagovsup")
dat.vparty.sbst <- subset(dat.vparty.full, year>=1970, select=vparty.vars)

### add V-dem variables
# load V-dem data
dat.vdem.full <- vdem
# subset by relevant variables
vdem.vars <- c("v2elparlel","v2elloeldm","v2elloelsy","e_p_polity","v2x_api",
               "e_gdppc","country_id","year")
dat.vdem.sbst <- subset(dat.vdem.full, year>=1970, select=vdem.vars)
# merge into base dataset
dat.mrgd.p1 <- merge(dat.vparty.sbst,dat.vdem.sbst,by=c("country_id","year"))

## subset by oced membership + full democracy by V-Dem's polyarchy measure (additive ordinal version)
# subset by OECD
oecd <- c("Czech Republic","Slovakia","South Korea","Estonia","Luxembourg","Poland","Netherlands",
          "Austria","Sweden","Switzerland","Germany","Greece","Belgium","Israel","Ireland","France",
          "Portugal","Spain","Turkey","Slovenia","Lithuania","Norway","Latvia","Australia","Iceland",
          "Denmark","New Zealand","Costa Rica","Mexico","Chile","Hungary","United Kingdom","Colombia",
          "Japan","United States of America","Canada","Italy","Finland")
dat.mrgd.p2 <- subset(dat.mrgd.p1, country_name %in% oecd)
# subset by high democracy score on V-Dem's Additive polyarchy index (>=0.75 on a 0-1 scale)
dat.mrgd.p3 <- subset(dat.mrgd.p2, v2x_api>=0.75)
table(dat.mrgd.p2$country_name)
table(dat.mrgd.p3$country_name)

## merge with ParlGov's party family variable (using Party Facts for inter-dataset party codes)
# Use Party Facts generated file with Parlgov and V-Party
code.parl <- read.csv("partyfacts-parlgov-vparty.csv")
# keep only relevant variables
keep.parl.code <- c("partyfacts_id","dataset_party_id.x","country.x","name_short.x")
code.parl.short <- subset(code.parl,select = keep.parl.code)
# read full Parlgov parties dataset
dat.parlgov <- read.csv("parlgov parties.csv")
# merge common Party Facts id into Parlgov data
dat.parlgov.pfcode <- merge(dat.parlgov,
                            code.parl.short,                    
                            by.x = "party_id",
                            by.y = "dataset_party_id.x",
                            all.x = T)
# create shorter ParlGov file (only party family and id)
keep.parl.family <- c("partyfacts_id","party_name_short","family_id")
dat.parl.family <- subset(dat.parlgov.pfcode, select = keep.parl.family)
# Merge Parlgov with full V-Party dataset using common Party Facts id
dat.mrgd.p4 <- merge(dat.mrgd.p3,
                     dat.parl.family,
                     by.x = "pf_party_id",
                     by.y = "partyfacts_id",
                     all.x = T, all.y = F)
# replace "yet to be coded" family_id values with NA
dat.mrgd.p4$family_id <- ifelse(dat.mrgd.p4$family_id==28, NA, dat.mrgd.p4$family_id)

######################
# code new variables #
######################
### add period counter
dat.mrgd.p5 <- dat.mrgd.p4 %>%
  group_by(v2paid) %>%
  arrange(year) %>%
  mutate(period = match(year, unique(year))) %>%
  ungroup()
# remove duplicates
dat.mrgd.p5 <- dat.mrgd.p5[!duplicated(dat.mrgd.p5[c("v2paid","period")]),]

### add decade dummy
dat.mrgd.p5$decade <- ifelse(
  dat.mrgd.p5$year>=1970 & dat.mrgd.p5$year<=1979,1,
  ifelse(dat.mrgd.p5$year>=1980 & dat.mrgd.p5$year<=1989,2,
         ifelse(dat.mrgd.p5$year>=1990 & dat.mrgd.p5$year<=1999,3,
                ifelse(dat.mrgd.p5$year>=2000 & dat.mrgd.p5$year<=2009,4,
                       ifelse(dat.mrgd.p5$year>=2010 & dat.mrgd.p5$year<=2019,5,
                              NA)))))

### recode selection method variable
## moderate inclusiveness
dat.mrgd.p5$prmrs.dlgts <- case_match(dat.mrgd.p5$v2panom_ord,
                                      c(0,1) ~ 0,
                                      c(2,3,4) ~ 1
)
## strict inclusiveness
dat.mrgd.p5$prmrs.mmbrs <- case_match(dat.mrgd.p5$v2panom_ord,
                                      c(0,1,2) ~ 0,
                                      c(3,4) ~ 1
)
table(dat.mrgd.p5$prmrs.dlgts)
table(dat.mrgd.p5$prmrs.mmbrs)
## party-level dummy for switch between selection methods (for counting)
dat.mrgd.p5 = dat.mrgd.p5 %>% 
  group_by(v2paid) %>%
  arrange(period) %>%
  mutate(prmrs.dlgts.swtch = prmrs.dlgts - lag(prmrs.dlgts)) %>%
  ungroup()
dat.mrgd.p5$prmrs.dlgts.swtch[is.na(dat.mrgd.p5$prmrs.dlgts.swtch)] <- 0
dat.mrgd.p5 = dat.mrgd.p5 %>% 
  group_by(v2paid) %>%
  arrange(period) %>%
  mutate(prmrs.mmbrs.swtch = prmrs.mmbrs - lag(prmrs.mmbrs)) %>%
  ungroup()
dat.mrgd.p5$prmrs.mmbrs.swtch[is.na(dat.mrgd.p5$prmrs.mmbrs.swtch)] <- 0
table(dat.mrgd.p5$prmrs.dlgts.swtch)
table(dat.mrgd.p5$prmrs.mmbrs.swtch)
## see which parties
dat.mrgd.p5.sbst <- subset(dat.mrgd.p5,select=c("year","country_name","v2paenname",
                                                "prmrs.dlgts.swtch","prmrs.mmbrs.swtch",
                                                "prmrs.dlgts","prmrs.mmbrs","v2panom_ord"))

## create country-election-level dummy for switch by any other party in a given election cycle
# first, code dummy for any switch;  second, turn back to 0 for the party that switches 
# (so it only indicates switch in any other party)
dat.mrgd.p5 = dat.mrgd.p5 %>% 
  group_by(country_id, year) %>%
  mutate(prmrs.dlgts.swtch.other = as.integer(any((prmrs.dlgts.swtch==1) | (prmrs.dlgts.swtch==-1)))) %>%
  ungroup()
dat.mrgd.p5$prmrs.dlgts.swtch.other <- ifelse(dat.mrgd.p5$prmrs.dlgts.swtch.other==1 & dat.mrgd.p5$prmrs.dlgts.swtch==1,
                                              0, dat.mrgd.p5$prmrs.dlgts.swtch.other)
dat.mrgd.p5 = dat.mrgd.p5 %>% 
  group_by(country_id, year) %>%
  mutate(prmrs.mmbrs.swtch.other = as.integer(any((prmrs.mmbrs.swtch==1) | (prmrs.mmbrs.swtch==-1)))) %>%
  ungroup()
dat.mrgd.p5$prmrs.mmbrs.swtch.other <- ifelse(dat.mrgd.p5$prmrs.mmbrs.swtch.other==1 & dat.mrgd.p5$prmrs.mmbrs.swtch==1,
                                              0, dat.mrgd.p5$prmrs.mmbrs.swtch.other)

## code dummy for government membership after the elections (to be lagged for prior incumbancy)
dat.mrgd.p5$gov.mmbr <- ifelse(
  dat.mrgd.p5$v2pagovsup<=1,1,0
)

################
# save dataset #
################
write.csv(dat.mrgd.p5, "Party data for analysis.csv", row.names=FALSE)
